package com.xkdx.guangguang.fragment.user;

import java.util.HashMap;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.user.UserModifyNickNameModule.ModifyNickName;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.module.util.ErrorModule;

public class UserModifyNickNameFragment extends BaseFragment {
	private EditText name;
	private Button finish, back;

	private String userID;
	private String userToken;
	private UserSharePrefence sp;
	public static String json = "";
	private ProgressDialog modifyPwdLoading;
	private UserModifyNickNamePresenter presenter;
	private UserModifyNickNameModule module;
	private UserModifyNickNameAction actions;
	private View view;
	private ModifyNickName user;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater
				.inflate(R.layout.user_modify_nickname, container, false);
		findViews();

		return view;
	}

	private void findViews() {
		name = (EditText) view.findViewById(R.id.nick_name);
		finish = (Button) view.findViewById(R.id.nick_finish);
		back = (Button) view.findViewById(R.id.nick_back);
		sp = new UserSharePrefence(getActivity(), IConstants.SP_USER);
		userID = sp.getUserID();
		userToken = sp.getUserLoginToken();

		modifyPwdLoading = new ProgressDialog(getActivity());
		modifyPwdLoading.setMessage("用户注册中,请耐心等待...");
		modifyPwdLoading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		modifyPwdLoading.setCancelable(true);
		modifyPwdLoading.setCanceledOnTouchOutside(false);// 解决4.0以上系统触屏后执行onCanel方法
		modifyPwdLoading
				.setOnCancelListener(new DialogInterface.OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialog) {
						getActivity().finish();
					}
				});

		setValuse();
	}

	private void setValuse() {
		finish.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!checkName()) {
					return;
				}

				showLoading();

			}
		});

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentManager fm = getFragmentManager();
				fm.popBackStack();
			}
		});

	}
	@Override
	public void showLoading() {
		actions = new UserModifyNickNameAction(userID, userToken, name
				.getText().toString().trim());
		// actions.requestData = json.toString();
		module = new UserModifyNickNameModule(getActivity());
		presenter = new UserModifyNickNamePresenter(this);
		((ManagerActivity) getActivity()).addCurrentTask(presenter);
		presenter.setActitons(actions);
		presenter.setModule(module);
		presenter.execute();
		modifyPwdLoading.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (presenter != null) {
					presenter.cancel(true);
				}
				getFragmentManager().popBackStack();

			}
		});
		modifyPwdLoading.show();
	}

	/**
	 * 检查名字是否为空
	 * 
	 * @return
	 */
	private boolean checkName() {
		if (TextUtils.isEmpty(name.getText().toString().trim())) {
			Toast.makeText(getActivity(), "昵称不能为空...", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		return true;

	}

	public void showLoaded(HashMap<String, AbsModule> msg) {
		if (modifyPwdLoading != null && modifyPwdLoading.isShowing()) {
			modifyPwdLoading.dismiss();
		}

		if (msg.containsKey("0")) {
			user = ((UserModifyNickNameModule) msg.get("0")).modifynickname;
			if (user != null && user.getStatus() != null
					&& user.getStatus().equals("Success")) {
				Toast.makeText(getActivity(), "昵称修改成功...", Toast.LENGTH_SHORT)
						.show();
				sp.setUserNickName(name.getText().toString().trim());
				FragmentManager fm1 = getFragmentManager();
				if (fm1 != null) {
					fm1.popBackStack();
				}
			} else {
				Toast.makeText(getActivity(), user.getMessage(), 0).show();
			}

		} else {
			Toast.makeText(getActivity(),
					((ErrorModule) msg.get("1")).errorMessage,
					Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void findView() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setOnClick() {
		// TODO Auto-generated method stub
		
	}
}
